<template>
	<view>
		<tab-nav @switchStatus="switchStatus" :orderStaIndex="orderStaIndex" :orderStaArr="orderStaArr" :margTop="0"></tab-nav>
		<view class="recordWrap">
			<view class="recordTit">
				<template v-if="loadFlag">最近{{diffday}}天您学习过的<text class="subject">{{subName}}</text>知识点如下:</template>
				<template v-else>
					<loading></loading> 
				</template>
			</view>
			<view class="listWrap">
				<template v-if="isHasDataFlag == true">
					<scroll-view class="scrollView" scroll-y>
						<record-list :recList="recList" @goRecDet="goRecDet"></record-list>
					</scroll-view>
				</template>
				<template v-else-if="isHasDataFlag == false">
					<no-data></no-data>
				</template>
			</view>
			<record-tj :loadFlag="loadFlag" :recField="recField"></record-tj>
			<uni-drawer :visible="showRight" mode="right" :isHadFlDataFlag="true" :zIndex="999" @close="closeDrawer">
				<filters
					:currRole="currRole"
					:subArr="subArr"
					:subIndex="subIndex"
					:startDate="startDate"
					:endDate="endDate"
					@selectCurrSub="selectCurrSub"
					@sureFilter="sureFilter"
					@bindDateChange="bindDateChange"
					@bindDateChangeEnd="bindDateChangeEnd"
				/>
			</uni-drawer>
		</view>
	</view>
</template>

<script>
	import { getSelfSubject,getSelfLearnRecList } from '@/api/zhuxue'
	import TabNav from '@/components/tab-nav/tab-nav.vue'
	import RecordList from './components/RecordList'
	import RecordTj from './components/RecordTj'
	import Filters from './components/Filter'
	export default {
		components:{
			TabNav,
			RecordList,
			RecordTj,
			Filters
		},
		data() {
			return {
				orderStaArr:[
					{id:0,name:'全部'},
					{id:1,name:'未完成'},
					{id:2,name:'已完成'}
				],
				orderStaIndex:0,
				showRight: false,
				subArr:[{}],
				subIndex:0,
				subId:0,
				recList:[],
				isHasDataFlag:null,
				startDate:'',
				endDate:'',
				diffday:0,
				subName:'',
				recField:{},
				loadFlag:false,
				isFinishSta:0,
				currRole:''
			}
		},
		onLoad(options){
			this.currRole = options.currRole;
			this.getSelfSubjectList();
		},
		methods: {
			goRecDet(item){
				uni.navigateTo({
					url:'/pages/subIndex/learnRecDet/learnRecDet?studyLogId=' + item.studyLogId + '&loreName=' + item.loreName + '&currRole=' + this.currRole
				})
			},
			getSelfSubjectList(){
				var field = { account:uni.getStorageSync('token') };
				let contentStr = this.crypto.encrypt(JSON.stringify( field )); 
				getSelfSubject({content:contentStr}).then(res=>{
					this.subArr = res.data.datas.subjectList;
					this.subId = this.subArr[0].subjectId;
					this.getSelfLearnRecList();
				}).catch(err=>{
					console.log(err)
				});
			},
			getSelfLearnRecList(){
				var field = { account:uni.getStorageSync('token'),subId:this.subId,sDate:this.startDate,eDate:this.endDate,isFinish:this.isFinishSta };
				let contentStr = this.crypto.encrypt(JSON.stringify( field )); 
				//console.log(field)
				getSelfLearnRecList({content:contentStr}).then(res=>{
					//console.log(res)
					var tmpRes = res.data.datas;
					this.startDate = tmpRes.sDate;
					this.endDate = tmpRes.eDate;
					this.diffday = tmpRes.diffday;
					this.subName = tmpRes.subName;
					this.loadFlag = true;
					this.recField = { diffday:tmpRes.diffday,finishNum:tmpRes.finish,noFinishNum:tmpRes.noFinish,totalNum:tmpRes.allStudyLog,comRate:tmpRes.comRate };
					if(tmpRes.studyList.length > 0){
						this.isHasDataFlag = true;
						this.recList = tmpRes.studyList;
					}else{
						this.recList = [];
						this.isHasDataFlag = false;
					}
				}).catch(err=>{
					this.isHasDataFlag = false;
					console.log(err)
				});
			},
			bindDateChange(e){
				var tmpCurrDate = this.startDate;
				this.startDate = e;
				if(this.startDate > this.endDate){
					this.util.showToast('开始日期不能大于结束日期');
					this.startDate = tmpCurrDate;
					return;
				}
			},
			bindDateChangeEnd(e){
				var tmpEndDate = this.endDate;
				this.endDate =  e;
				if(this.startDate > this.endDate){
					this.util.showToast('开始日期不能大于结束日期');
					this.endDate = tmpEndDate;
					return;
				}
			},
			onNavigationBarButtonTap(){
				this.showRight = true;
			},
			sureFilter(){
				this.showRight = false;
				this.getSelfLearnRecList();
			},
			switchStatus(index,id){
				this.orderStaIndex = index;
				this.isFinishSta = id;
				this.getSelfLearnRecList();
			},
			selectCurrSub(index,item){
				this.showRight = false;
				this.subIndex = index;
				this.subId = item.subjectId;
				this.getSelfLearnRecList();
			},
			closeDrawer(){
				this.showRight = false;
			}
		}
	}
</script>
<style lang="scss">
	page{
		background: $pss-bg-primary;
	}
</style>
<style lang="scss" scoped>
	@import '@/static/scss/learnRecord.scss';
</style>
